//
//  XPMessageQueueViewController.m
//  QQ
//
//  Created by nhope on 2017/4/26.
//  Copyright © 2017年 xiaopin. All rights reserved.
//

#import "XPMessageQueueViewController.h"
#import "XPMessageQueueCellView.h"
#import "XPMessageContactModel.h"

@interface XPMessageQueueViewController ()<NSTableViewDataSource, NSTableViewDelegate>

@property (weak) IBOutlet NSTableView *tableView;
@property (nonatomic, strong) NSMutableArray<XPMessageContactModel *> *contacts;

@end

@implementation XPMessageQueueViewController

#pragma mark - Lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 模拟联系人数据
    _contacts = [NSMutableArray array];
    for (int i=0; i<100; i++) {
        XPMessageContactModel *contact = [[XPMessageContactModel alloc] init];
        contact.avatar = @"login-avatar";
        contact.username = [NSString stringWithFormat:@"UserName-%d", i];
        contact.message = [NSString stringWithFormat:@"Message for index %d.", i];
        contact.selected = NO;
        [_contacts addObject:contact];
    }
}

#pragma mark - <NSTableViewDataSource>

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
    return self.contacts.count;
}

#pragma mark - <NSTableViewDelegate>

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
    XPMessageQueueCellView *view = (XPMessageQueueCellView*)[tableView makeViewWithIdentifier:@"cell" owner:nil];
    XPMessageContactModel *contact = self.contacts[row];
    [view showWithContact:contact];
    __weak __typeof(self) weakSelf = self;
    [view setDidClickedCloseButtonBlock:^(XPMessageQueueCellView *cellView){
        [weakSelf removeContactForCellView:cellView];
    }];
    return view;
}

- (BOOL)selectionShouldChangeInTableView:(NSTableView *)tableView {
    if (tableView.numberOfSelectedRows) {
        XPMessageContactModel *contact = self.contacts[tableView.selectedRow];
        contact.selected = NO;
        XPMessageQueueCellView *view = (XPMessageQueueCellView*)[tableView viewAtColumn:0 row:tableView.selectedRow makeIfNecessary:NO];
        [view showWithContact:contact];
    }
    return YES;
}

- (void)tableViewSelectionDidChange:(NSNotification *)notification {
    NSTableView *tableView = (NSTableView *)notification.object;
    NSInteger index = tableView.selectedRow;
    XPMessageContactModel *contact = self.contacts[index];
    contact.selected = YES;
    XPMessageQueueCellView *view = (XPMessageQueueCellView*)[tableView viewAtColumn:0 row:index makeIfNecessary:NO];
    [view showWithContact:contact];
    // TODO:切换对应的聊天界面
}

#pragma mark - Private

- (void)removeContactForCellView:(XPMessageQueueCellView *)cellView {
    NSInteger index = [self.tableView rowForView:cellView];
    [self.contacts removeObjectAtIndex:index];
    [self.tableView removeRowsAtIndexes:[NSIndexSet indexSetWithIndex:index]
                          withAnimation:NSTableViewAnimationEffectFade];
    // TODO:如果当前关闭的联系人刚好是正在聊天的联系人,则需要更新右侧发消息页面控制器UI
}

@end
